package worklog.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import worklog.db.UserDB;
import worklog.dto.ResponseDTO;

/**
 * Handles get-project requests
 */

@WebServlet("/users")
public class Users extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private UserDB userDB;
	private ResponseDTO responder = new ResponseDTO();

    public Users() throws Exception{ super(); }
    
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
		//doGet(request, response); 
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
		userDB = new UserDB();
		PrintWriter out = response.getWriter();
		response.setContentType("application/json");
    	if (userDB.isReady()) out.print(processRequest(request));
    	else out.print(responder.showError("DB connection error"));
    	userDB.close();
    	out.close();
	}
	
	private String processRequest(HttpServletRequest request){
    	String users= "";
    	try {
    		users = userDB.getUsers().toString();
		} 
    	catch (SQLException e) {}
    	return users;
	}
}